package com.ev.apis.controller.custom;

import com.ev.apis.model.DsResultResponse;
import com.ev.custom.domain.FacilityDO;
import com.ev.custom.service.FacilityService;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.annotation.RecoverRemoveData;
import com.ev.framework.config.Constant;
import com.ev.framework.il8n.MessageSourceHandler;
import com.ev.framework.utils.R;
import com.ev.framework.utils.ShiroUtils;
import com.ev.framework.utils.StringUtils;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@Api(value = "/", tags = "仓库管理")
public class FacilityApiController {
    @Autowired
    private FacilityService facilityService;
    @Autowired
    private MessageSourceHandler messageSourceHandler;

    @EvApiByToken(value = "/apis/facility/list",method = RequestMethod.GET,apiTitle = "获取仓库列表信息")
    @ApiOperation("获取仓库列表信息")
    public R list(@ApiParam(value = "当前第几页",required = true) @RequestParam(value = "pageno",defaultValue = "1") int pageno,
                  @ApiParam(value = "一页多少条",required = true) @RequestParam(value = "pagesize",defaultValue = "20") int pagesize,
                  @ApiParam(value = "仓库名称") @RequestParam(value = "name",defaultValue = "",required = false)  String name,

                  @ApiParam(value = "审核状态") @RequestParam(value = "auditSign",defaultValue = "",required = false)  String auditSign,
                  @ApiParam(value = "是否显示已删除数据") @RequestParam(value = "showDeleteData", defaultValue = "0", required = false) Integer showDeleteData,
                  @ApiParam(value = "组织代码") @RequestParam(value = "orgNumber", defaultValue = "") String orgNumber){
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(3);
        params.put("name", StringUtils.sqlLike(name));

        params.put("auditSign", auditSign);

        params.put("showDeleteData", showDeleteData);

        params.put("offset",(pageno-1)*pagesize);
        params.put("limit",pagesize);
        params.put("orgNumber", orgNumber);
        Map<String,Object> results = Maps.newHashMap();
        List<Map<String,Object>> data= this.facilityService.listForMap(params);
        int total = this.facilityService.countForMap(params);
        if(data!=null && data.size()>0){
            results.put("data", new DsResultResponse(pageno,pagesize,total,data));
        }
        return  R.ok(results);
    }

    @EvApiByToken(value = "/apis/facility/detail",method = RequestMethod.GET,apiTitle = "获取仓库详细信息")
    @ApiOperation("获取仓库详细信息")
    public R detail(@ApiParam(value = "主键ID",required = true) @RequestParam(value = "id",defaultValue = "")  Long id) {
        Map<String, Object> result = Maps.newHashMap();
        Map<String, Object> detail = facilityService.getById(id);
        if (detail != null && detail.size() > 0) {
            result.put("data", detail);
        }
        return R.ok(result);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/save",method = RequestMethod.POST,apiTitle = "保存仓库信息")
    @ApiOperation("保存仓库信息")
    public R save(@ApiParam(value = "仓库信息",required = true) FacilityDO facilityDO){
        return facilityService.add(facilityDO);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/update",method = RequestMethod.POST,apiTitle = "修改仓库信息")
    @ApiOperation("修改仓库信息")
    public R update(@ApiParam(value = "仓库信息",required = true) FacilityDO facilityDO){
    	return facilityService.edit(facilityDO);
    }
    
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/remove",method = RequestMethod.POST,apiTitle = "删除仓库信息")
    @ApiOperation("删除仓库信息")
    public R remove(@ApiParam(value = "仓库主键",required = true) @RequestParam(value="id",defaultValue = "") Long id){
        FacilityDO facilityDO = facilityService.get(id);
        if (Objects.equals(facilityDO.getAuditSign(), Constant.OK_AUDITED)) {
            return R.error(messageSourceHandler.getMessage("common.approved.delete.disabled",null));
        }
        if(facilityService.logicRemove(id)>0){
            return R.ok();
        }
        return R.error();
    }
    
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/batchRemove",method = RequestMethod.POST,apiTitle = "批量删除仓库信息")
    @ApiOperation("批量删除仓库信息")
    public R remove(@ApiParam(value = "仓库主键数组",required = true, example = "[1,2,3,4]") @RequestParam(value="ids",defaultValue = "") Long[] ids){
        for (Long id : ids) {
            FacilityDO facilityDO = facilityService.get(id);
            if (Objects.equals(facilityDO.getAuditSign(), Constant.OK_AUDITED)) {
                return R.error(messageSourceHandler.getMessage("common.approved.delete.disabled",null));
            }
        }
        facilityService.logicBatchRemove(ids);
        return R.ok();
    }


    @RecoverRemoveData(tableName = "cus_facility",columnName = "del_flag",columnValue = "0")
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/recoverRemoveData",method = RequestMethod.POST,apiTitle = "恢复删除信息")
    @ApiOperation("恢复删除信息")
    public R recoverRemoveData(@ApiParam(value = "仓库主键数组",required = true, example = "[1,2,3,4]") @RequestParam(value="ids",defaultValue = "") Long[] ids){
        // 切片统一处理
        return R.ok();
    }

    /**
     * 审核仓库
     *
     * @date 2019-12-03
     * @author gumingjie
     */
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/audit", method = RequestMethod.POST, apiTitle = "审核仓库")
    @ApiOperation("审核仓库")
    public R audit(
            @ApiParam(value = "仓库主键", required = true) @RequestParam(value = "id", defaultValue = "") Long id) {
        FacilityDO facilityDO = facilityService.get(id);
        if (Objects.equals(facilityDO.getAuditSign(), Constant.OK_AUDITED)) {
            return R.error(messageSourceHandler.getMessage("common.duplicate.approved",null));
        }
        if (facilityService.audit(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/batchAudit", method = RequestMethod.POST, apiTitle = "审核仓库")
    @ApiOperation("批量审核仓库")
    public R audit(
            @ApiParam(value = "仓库主键数组", required = true) @RequestParam(value = "ids", defaultValue = "") Long[] ids) {
        if (ids == null || ids.length == 0) {
            return R.error();
        }
        Map<String,Object> map = Maps.newHashMap();
        map.put("idList", Arrays.asList(ids));
        List<FacilityDO> list = facilityService.list(map);
        // 若存在已审核状态
        boolean b = list.stream().anyMatch(e -> Objects.equals(e.getAuditSign(), Constant.OK_AUDITED));
        if (b) {
            return R.error(messageSourceHandler.getMessage("common.duplicate.approved",null));
        }
        list.forEach(e->{
            e.setAuditor(ShiroUtils.getUserId());
            e.setAuditSign(Constant.OK_AUDITED);
        });
        facilityService.batchUpdate(list);

        return R.ok();
    }

    /**
     * 反审核仓库
     *
     * @date 2019-12-03
     * @author gumingjie
     */
    @Transactional(rollbackFor = Exception.class)
    @EvApiByToken(value = "/apis/facility/reverseAudit", method = RequestMethod.POST, apiTitle = "反审核仓库")
    @ApiOperation("反审核仓库")
    public R reverseAudit(
            @ApiParam(value = "仓库主键", required = true) @RequestParam(value = "id", defaultValue = "") Long id) {
        FacilityDO facilityDO = facilityService.get(id);
        if (Objects.equals(facilityDO.getAuditSign(), Constant.WAIT_AUDIT)) {
            return R.error(messageSourceHandler.getMessage("receipt.reverseAudit.nonWaitingAudit",null));
        }
        if (facilityService.reverseAudit(id) > 0) {
            return R.ok();
        }
        return R.error();
    }
}
